// pages/asc/apple/applyTypeSummer/applyTypeSummer.js
var _utils = require('../../../../utils/utils.js')
var _http = require('../../../../utils/httpUtils.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        introduce: '', //夏季项目介绍
        program: "", // 上个页面选择的项目
        professor: "", //教授
        pic1: "", //图片1的回显路径
        pic2: '', //图片2的回显路径
        transcript:'', //上传图片1的返回路径
        report_card:'',//上传图片1的返回路径
        addActBtn:0, //添加活动按钮状态
        //是否已签约升学顾问
        //监护人关系
        counseling_index: 0,
        counselingArr: [{
            id: 0,
            name: '请选择'
        }, {
            id: 1,
            name: '是'
        }, {
            id: 2,
            name: '否'
        }],
        //在申请添加的学术
        academic_interests_index: 0,
        academic_interestsArr: [{
            id: 0,
            name: '请选择'
        }, {
            id: 1,
            name: '学术1'
        }, {
            id: 2,
            name: '学术2'
        }, {
            id: 3,
            name: '学术3'
        }],
        // 在申请参加的项目
        asc_programs_index: 0,
        asc_programsArr: [{
            id: 0,
            name: '请选择'
        }, {
            id: 1,
            name: '1V1科研'
        }, {
            id: 2,
            name: '线上科研'
        }, {
            id: 3,
            name: 'ASC 夏季项目'
        }, {
            id: 3,
            name: '纽约实地科研项⽬'
        }],
        //bindcurrent_gradeChange 目前年级
        current_grade_index: 0,
        current_gradeArr: [{
            id: 0,
            name: '请选择'
        }, {
            id: 1,
            name: '高一'
        }, {
            id: 2,
            name: '高二'
        }, {
            id: 3,
            name: '高三'
        }],
        //学校类型
        school_type_index: 0,
        school_typeArr: [{
            id: 0,
            name: '请选择'
        }, {
            id: 1,
            name: '公立'
        }, {
            id: 2,
            name: '私立'
        }],
        //语言等级
        language_ability_index: 0,
        language_abilityArr: [{
            id: 0,
            name: '请选择'
        }, {
            id: 1,
            name: '四级'
        }, {
            id: 2,
            name: '六级'
        }, {
            id: 3,
            name: '八级'
        }],
        //监护人关系
        relationship_index: 0, //默认显示位置
        relationshipArr: [{
            id: 0,
            name: '请选择'
        }, {
            id: 1,
            name: '父亲'
        }, {
            id: 2,
            name: '母亲'
        }],
        //出生地
        birthplaceRegion: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        if (options) {
            var program = options.program;
            var t = options.t;
            this.setData({
                program: program,
                professor: t,
                course_id:options.kcId,
                cate_id:options.cate_id,
            })
        }
        //获取顶部的介绍信息
        getTopInfo(this);
    },
    //选择图片
    choosePics: function (e) {
        let imgt = e.currentTarget.dataset.img;
        let _this = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'],
            success: function (res) {
                if (imgt == 1) {
                    _this.setData({
                        pic1: res.tempFilePaths
                    })
                } else {
                    _this.setData({
                        pic2: res.tempFilePaths
                    })
                }
            }
        })
    },
    uploadPic:function(e){
        let imt = e.currentTarget.dataset.img;
        let _this = this;
        let file ;
        _utils.toast("正在上传...",2,10000)
        if(imt == 1){
            if(!_this.data.pic1){
                _utils.toast("请先选择图片");
                return;
            }
            file = _this.data.pic1[0];
        }else{
            if(!_this.data.pic2){
                _utils.toast("请先选择图片");
                return;
            }
            file = _this.data.pic2[0];
        }

        let arr =  file.toString().split('.');
        console.log(arr);
        wx.uploadFile({  
            url: _http.uploadImg,  
            filePath: file,  
            name: 'upload',  
            header: {
                "Content-Type": "multipart/form-data"
            },
            formData: {ext:arr[arr.length-1]},   
            success: function (res) {  
              console.log(res)  
              console.log(JSON.parse(res.data));
              let data = JSON.parse(res.data);
              if (data.status ==1) {  
               
                if(imt==1){
                    _this.setData({
                        transcript:data.data.pic
                    })
                }else{
                    _this.setData({
                        report_card:data.data.pic
                    })
                }
                console.log(_this.data.transcript);
                wx.showToast({  
                    title: '上传成功',
                    icon:"success"  ,
                    duration: 2000  
                  });  
              } else{
                wx.showToast({  
                    title: '上传失败:'+data.msg,  
                    icon:'none',
                    duration: 2000  
                  }); 
              }
            },fail:function(res){
                wx.showToast({  
                    title: '上传失败:'+res.errMsg,  
                    icon:'none',
                    duration: 2000  
                  });  
            }
            ,complete:function(res){}
          })  
    },
    //日期选择器：
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value
        })
    },
     //活动日期选择器：
     bindactdateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        let act = e.currentTarget.dataset.act;
        console.log(act);
        if(act == 1){
            this.setData({
                actdate1: e.detail.value
            })
        }else if(act == 2){
            this.setData({
                actdate2: e.detail.value
            })
        } else{
            this.setData({
                actdate3: e.detail.value
            })
        }
    },
    //活动输入框
    actInput:function(e){
        let act = e.currentTarget.dataset.act;
        if(act == 1){
            this.setData({
                acttitle1: e.detail.value
            })
        }else if(act == 2){
            this.setData({
                acttitle2: e.detail.value
            })
        } else{
            this.setData({
                acttitle3: e.detail.value
            })
        }
    },
    //活动添加按钮
    addActBtn:function(e){
        let btn = e.currentTarget.dataset.btn;
        this.setData({
            addActBtn:btn
        })
    },
    //是否已签约升学顾问
    bindcounselingChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            counseling_index: e.detail.value,
        })

    },
    //在申请参加的学术
    bindacademic_interestsChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            academic_interests_index: e.detail.value,
        })

    },
    //监护人关系
    bindrelationshipChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            relationship_index: e.detail.value,
        })
    },
    //在申请参加的项目
    bindasc_programsChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            asc_programs_index: e.detail.value,
        })
    },
    // 目前年级
    bindcurrent_gradeChange: function (e) {
        this.setData({
            current_grade_index: e.detail.value,
        })
    },
    //出生地
    bindbirthplaceChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            birthplaceRegion: e.detail.value
        })
    },
    //bindlanguage_abilityChange
    // 目前年级
    bindlanguage_abilityChange: function (e) {
        this.setData({
            language_ability_index: e.detail.value,
        })
    },
    //学校类型
    bindschool_typeChange: function (e) {
        this.setData({
            school_type_index: e.detail.value,
        })
    },

    // 提交申请
    formSubmit: function (e) {
        console.log(e.detail.value);
        let formdata = e.detail.value;
        let  flat = _utils.checkFormNull(formdata,['activities3','activities2','grade_level','actdate2','actdate3','acttitle2','acttitle3','asc_programs_index','asc_programs','course_id','cate_id']);
        console.log("检测完空值返回值:"+flat);
        // if (flat) {
            //开始请求接口
            _http.post(_http.enroll, e.detail.value, function (res) {
                if (res.status == 1) {
                    wx.navigateBack({
                        complete: (res) => { },
                    })

                    wx.navigateTo({
                        url: '../applyStatus/applyStatus?stat=1',
                        success: function (res) { },
                        fail: function (res) { },
                        complete: function (res) { },
                    })
                }

            })
        // }
    },
    //省市区选择器：
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
function getTopInfo(_this) {
    _http.post(_http.enrollDes, {}, function (res) {
        _this.setData({
            introduce: res.data
        })
    })
}